﻿using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using HeritageAcademy.Models.DataModels;
using HeritageAcademy.Shared;

namespace HeritageAcademy.Models.ViewModels
{
    public class TableOfContentsViewModel : ViewModelBase<Course>
    {
        #region Parameters 
        
        private readonly CourseViewModel _bookViewModel;
        private ObservableCollection<CourseLocationViewModel> _bookmarks;
        
        #endregion

        #region Ctor 
        
        public TableOfContentsViewModel(CourseViewModel b) : base(b.Model)
        {
            _bookViewModel = b;

            // find the navigable chapters
            Chapters = new ObservableCollection<ChapterViewModel>();
            foreach (var c in b.FlatChapters.Where(c => c.Chapters == null || c.Chapters.Count == 0))
                Chapters.Add(c);
        }
        
        #endregion

        #region Properties 
        
        public ObservableCollection<CourseLocationViewModel> Bookmarks
        {
            get { return _bookmarks ?? (_bookmarks = new ObservableCollection<CourseLocationViewModel>()); }
            set
            {
                _bookmarks = value;
                RaisePropertyChanged("Bookmarks");
            }
        }

        public ObservableCollection<ChapterViewModel> Chapters { get; set; }

        private CourseLocation FurthestReadLocation
        {
            get
            {
                if (Model == null || Model.UserMetadata == null) return null;
                return Model.UserMetadata.FurthestReadLocation;
            }
        }

        private CourseLocation LastReadLocation
        {
            get
            {
                if (Model == null || Model.UserMetadata == null) return null;
                return Model.UserMetadata.LastReadLocation;
            }
        }
        
        #endregion Properties 

        #region GotoFurthestLocationCommand
        
        private RelayCommand _gotoFurtherstLocationCommand;

        public ICommand GotoFurtherstLocationCommand
        {
            get
            {
                return _gotoFurtherstLocationCommand ?? 
                            (_gotoFurtherstLocationCommand = new RelayCommand(
                                                                            param => GotoFurthestLocation(),
                                                                            param => CanGotoFurthestLocation()));
            }
        }

        private void GotoFurthestLocation()
        {
            if (_bookViewModel != null && FurthestReadLocation != null)
                _bookViewModel.CurrentLocation = FurthestReadLocation;
        }

        private bool CanGotoFurthestLocation()
        {
            return (_bookViewModel != null && FurthestReadLocation != null);
        }
        
        #endregion

        #region GotoBeginningCommand
        
        private RelayCommand _gotoBeginningCommand;

        public ICommand GotoBeginningCommand
        {
            get
            {
                return _gotoBeginningCommand ?? (_gotoBeginningCommand = new RelayCommand(
                                                                             param => GotoBeginning(),
                                                                             param => CanGotoBeginning()));
            }
        }

        private void GotoBeginning()
        {
            if (_bookViewModel != null && FurthestReadLocation != null)
                _bookViewModel.CurrentLocation = new CourseLocation(0, 0);
        }

        private bool CanGotoBeginning()
        {
            return (_bookViewModel != null && FurthestReadLocation != null);
        }
        
        #endregion
    }
}